package Thread;

import java.util.concurrent.atomic.AtomicInteger;

//CAS(原子类的使用);
//作用：①：性能好；
//    ②：不用加锁也可以解决线程安全问题；
public class Demo39 {
    //public static int count=0;
    //将count设置成一个原子类；
    public static AtomicInteger count=new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            for(int i=0;i<5000;i++){
                //count++;
                count.getAndIncrement();
                //次方法相当于count++;
                //count.incrementAndGet();
                //此方法相当于++count;
                //int n=0;
                //count.addAndGet(n);
                //此方法相当于：count+=n;
            }
        });
        Thread t2=new Thread(()->{
            for(int i=0;i<5000;i++){
                count.getAndIncrement();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count的值为"+count.get());
    }
}
